package com.oil.petro.eshop.dao;

import com.oil.petro.eshop.entity.Product;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

@Repository("ProductDAO")
public class ProductDAOImpl implements ProductDAO {

    @Autowired
    private SessionFactory sessionfactory;

    @Override
    public Product get(int id) {
        return (Product) sessionfactory.getCurrentSession().get(Product.class, id);
    }

    @Override
    public void delete(Product product) {
        sessionfactory.getCurrentSession().delete(product);
    }
    
    @Override
    public void delete(int productId){
    Product product = get(productId);
        sessionfactory.getCurrentSession().delete(product);
    }
    
    @Override
    public int save(Product product) {
        return (int) sessionfactory.getCurrentSession().save(product);
    }

    @Override
    public void update(Product product) {
        sessionfactory.getCurrentSession().update(product);
    }

//    @Override
//    public List<Product> getList() {
//        return sessionfactory.getCurrentSession().createCriteria(Product.class).list();
//    }
    @Override
    public List<Product> getList(int amount, int page) {
        //return sessionfactory.getCurrentSession().createCriteria()
        // String hql = "from Product LIMIT " + amount + " OFFSET " + amount * page;
        Query query = sessionfactory.getCurrentSession().createQuery("from Product");
        query.setFirstResult(page * amount);//set first result start value  
        query.setMaxResults(amount);

        return query.list();
    }
    
    @Override
    public List<Product> getList() {
        //return sessionfactory.getCurrentSession().createCriteria()
        // String hql = "from Product LIMIT " + amount + " OFFSET " + amount * page;
        Query query = sessionfactory.getCurrentSession().createQuery("from Product");
        return query.list();
    }
    
    @Override
    public List<Product> getList(int status) {
        //return sessionfactory.getCurrentSession().createCriteria()
        // String hql = "from Product LIMIT " + amount + " OFFSET " + amount * page;
        Query query = sessionfactory.getCurrentSession().createQuery("from Product where order_status="+status);
        return query.list();
    }

    public List<Product> getProductListForCategory(int categoryId) {
        return sessionfactory.getCurrentSession().createQuery("from Product WHERE category_id=" + categoryId).list();
    }

    public List<Product> searchProductList(int categoryId, int manufacturerId, boolean orderFlag, int amount, int pageNumber) {
        String order = (orderFlag == false) ? "ASC" : "DESC";
        String searchWithCategory = "from Product WHERE category_id=" + categoryId + " ORDER BY cost " + order;
        String searchWithManufacturer = "from Product WHERE manufacturer_id=" + manufacturerId + " ORDER BY cost " + order;
        String searchForPrice = "from Product ORDER BY cost " + order;
        String searchBoth = "from Product WHERE manufacturer_id=" + manufacturerId + "AND category_id=" + categoryId + " ORDER BY cost " + order;
        Query query;
        if (categoryId > 0 && manufacturerId == -1) {
            query = sessionfactory.getCurrentSession().createQuery(searchWithCategory);
            query.setFirstResult(pageNumber * amount);//set first result start value  
            query.setMaxResults(amount);
        } else if (manufacturerId > 0 && categoryId == -1) {
            query = sessionfactory.getCurrentSession().createQuery(searchWithManufacturer);
            query.setFirstResult(pageNumber * amount);//set first result start value  
            query.setMaxResults(amount);
        } else if (manufacturerId > 0 && categoryId > 0) {
            query = sessionfactory.getCurrentSession().createQuery(searchBoth);
            query.setFirstResult(pageNumber * amount);//set first result start value  
            query.setMaxResults(amount);
        } else {
            query = sessionfactory.getCurrentSession().createQuery(searchForPrice);
            query.setFirstResult(pageNumber * amount);//set first result start value  
            query.setMaxResults(amount);
        }
        return query.list();

    }    

}
